﻿Imports ADOEntity
Imports System.Data
Partial Class AML_systemConfig
    Inherits Web.UI.BasePage
    Private Const strMenuCode As String = "M61"

    Dim systemConfigEntity As New systemConfigEntities
    Dim dsSystemConfig As New DataSet
    Dim clsQuery As clsDatabase
    Dim clsDataTable As New clsLINQToDataTable
    Dim clsSessionMenuRight As clsSessionMenuRight

#Region " Even Page "
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        btnSystemConfig.Text = Resources.resSystemConfig.btnModifyConfig

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        chkIsLogin()
        clsSessionMenuRight = Session("ClsMenuRight")

        If (Not IsPostBack) Then
            setUserRightButton()
            IsDataBind()
            CreateControlMP()
        Else
            dsSystemConfig = Session("dsTblMaster")
            gvSystemConfig.DataSource = dsSystemConfig.Tables("sSystemConfig")
            gvSystemConfig.DataBind()
        End If
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("dsTblMaster") = dsSystemConfig
    End Sub

#End Region

#Region "SetForm"

    Sub CreateControlMP()

        Dim lblForm As Label = CType(Master.FindControl("lblForm"), Label)
        lblForm.Text = "ตั้งค่าระบบ"
    End Sub

#End Region

#Region " Even Button "

    Protected Sub btnSystemConfig_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSystemConfig.Click
        Response.Redirect("systemConfigEdit.aspx")
    
    End Sub
#End Region

#Region "Even GridView"

    Protected Sub gvSystemConfig_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvSystemConfig.SelectedIndexChanged
        ' If Not (gvShowPosition.SelectedIndex = -1) Then
        'btnDelete.Attributes.Add("onclick", "return confirm('" + String.Format(Resources.resValidate.confirmDelete, _
        '                                                                 IIf(gvShowPosition.SelectedIndex = -1, "", gvShowPosition.Rows(gvShowPosition.SelectedIndex).Cells(2).Text)) + "');")
        '   End If

    End Sub

    Protected Sub gvSystemConfig_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvSystemConfig.PageIndexChanging
        gvSystemConfig.PageIndex = e.NewPageIndex
        clearGvIndex()
        DataBind()
    End Sub

    Protected Sub gvSystemConfig_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvSystemConfig.Sorting
        Dim dt = TryCast(dsSystemConfig.Tables("sSystemConfig"), DataTable)

        If dt IsNot Nothing Then

            'Sort the data.
            dt.DefaultView.Sort = e.SortExpression & " " & GetSortDirection(e.SortExpression)
            gvSystemConfig.DataSource = dsSystemConfig.Tables("sSystemConfig")
            gvSystemConfig.DataBind()
            clearGvIndex()
        End If
    End Sub

#End Region

#Region " Function"
    Private Sub setUserRightButton()
        Dim col As New Collection
        col.Add(btnSystemConfig, statusbtn.Modify)
        col.Add(btnPrint, statusbtn.print)
        setBtnToUserRight(col, strMenuCode, clsSessionMenuRight.getDsMenuRight)

    End Sub
    Private Sub clearGvIndex()
        gvSystemConfig.SelectedIndex = -1
        'btnSystemConfig.Attributes.Clear()
    End Sub


    'Private Sub IsDataBind()

    '    If dsSystemConfig.Tables.Contains("sSystemConfig") Then
    '        dsSystemConfig.Tables.Clear()
    '    End If


    '    Dim strSQL As String = ""
    '    clsQuery = New clsDatabase

    '    strSQL = " select configCode,configDesc,configNum,configstr  " _
    '                & " from sSystemConfig " _
    '                & "order by configCode"

    '    dsSystemConfig = clsQuery.QueryDataSet(strSQL, "sSystemConfig")

    '    gvSystemConfig.DataSource = dsSystemConfig.Tables("sSystemConfig")
    '    gvSystemConfig.DataBind()


    '    clsQuery = Nothing

    'End Sub
    Private Sub IsDataBind()
        If dsSystemConfig.Tables.Contains("sSystemConfig") Then
            dsSystemConfig.Tables.Clear()
        End If

        Dim qrySystemConfig = From stcf In systemConfigEntity.SSYSTEMCONFIG _
                         Order By stcf.CONFIGINDEX
                         Select stcf.CONFIGCODE, stcf.CONFIGDESC, stcf.CONFIGNUM, stcf.CONFIGSTR, stcf.CONFIGINDEX



        dsSystemConfig.Tables.Add(clsDataTable.LINQToDataTable(qrySystemConfig, "sSystemConfig").Copy)

        'If (qrySystemConfig.Count = 0) Then
        '    lblMsg.Visible = True
        'Else
        '    lblMsg.Visible = False

        'End If

        gvSystemConfig.DataSource = dsSystemConfig.Tables("sSystemConfig")
        gvSystemConfig.DataBind()

        clsQuery = Nothing
    End Sub

    Private Function GetSortDirection(ByVal column As String) As String

        ' By default, set the sort direction to ascending.
        Dim sortDirection = "DESC"

        ' Retrieve the last column that was sorted.
        Dim sortExpression = TryCast(ViewState("SortExpression"), String)

        If sortExpression IsNot Nothing Then
            ' Check if the same column is being sorted.
            ' Otherwise, the default value can be returned.
            If sortExpression = column Then
                Dim lastDirection = TryCast(ViewState("SortDirection"), String)
                If lastDirection IsNot Nothing _
                  AndAlso lastDirection = "DESC" Then

                    sortDirection = "ASC"

                End If
            End If
        End If

        ' Save new values in ViewState.
        ViewState("SortDirection") = sortDirection
        ViewState("SortExpression") = column
        Return sortDirection
    End Function

    Sub saveLogPrint()
        Dim desc As String = "พิมพ์ข้อมูลตั้งค่าระบบ"

        Dim clsLogTransaction As New clsLogTransaction
        clsLogTransaction.saveLogTransaction("", clsSessionMenuRight.getUserName, 2, desc, "M61")
        clsLogTransaction = Nothing
    End Sub

#End Region


    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        saveLogPrint()
        Dim param As String = "{ 'rptType' : '1'}"
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('../Form/frmPrintRptMaster.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)
    End Sub
End Class
